<?php

class Elective_CopyController extends Zend_Controller_Action
{

    private function _init()
    {
        /* Initialize action controller here */
        // 目前的學年學期
        $this->_year = $this->view->Semester['year'];
        $this->_semester = $this->view->Semester['semester'];
    }

    public function indexAction()
    {
        // action body
    }

    public function getSubjectAction()
    {
        $this->getHelper('layout')->disableLayout();
        $this->_init();
        $grade = (int) $this->getRequest()->getParam('grade');

        $q = Doctrine_Query::create()
        ->select('a.sn, a.group_name , a.member, b.subject_sn AS subject_sn, c.name AS name,
        d.name AS teacher_name, count(e.sn) AS cc')
        ->from('Student_Model_StudentScoreElectiveGroup a')
        ->leftJoin('a.Student_Model_StudentScoreElectiveStudent e')
        ->leftJoin('a.Semester_Model_SemesterTeacherCourse b')
        ->leftJoin('b.Semester_Model_SemesterCourse c')
        ->leftJoin('Teacher_Model_TeacherView d')
        ->where('b.year=?', $this->_year)
        ->andWhere('b.semester=?', $this->_semester)
        ->andWhere('b.grade=?', $grade)
        ->andWhere('b.teacher_sn=d.sn')
        ->groupBy('a.sn');
        //->groupBy()
        $res = $q->fetchArray();
        $arr = array();
        foreach($res as $row)
        $arr[$row['name']][$row['sn']] = $row['group_name'].' -- '. $row['teacher_name'].
        ' ('.$row['cc'].'/'.$row['member'].')';


        $this->view->data = $arr;
    }

    public function saveAction()
    {
        $this->getHelper('layout')->disableLayout();
        $this->getHelper('viewRenderer')->setNoRender();

        $source = (int) $this->_getParam('source');
        $target = (int) $this->_getParam('target');
//echo $source.'--'.$target; exit;
        // 先刪除原有資料
        Doctrine_Query::create()
        ->delete('Student_Model_StudentScoreElectiveStudent')
        ->where('group_sn=?', $target)->execute();

        // 複製來源資料
        $res = Doctrine_Query::create()
        ->from('Student_Model_StudentScoreElectiveStudent')
        ->where('group_sn=?', $source)->fetchArray();

        foreach ($res as $row) {
            $s = new Student_Model_StudentScoreElectiveStudent();
            $s->group_sn = $target;
            $s->student_sn = $row['student_sn'];
            $s->number = $row['number'];
            $s->save();
        }
        echo 'ok';

    }


    public function getCopyDataAction()
    {
        $this->getHelper('layout')->disableLayout();
        $this->_init();
        $sn = (int) $this->_getParam('sn');
        $grade = (int) $this->_getParam('grade');
        $onlySemester = (int) $this->_getParam('only_semester');
        $this->view->only_semester = $onlySemester;
         // 找出科目代號
         $subjectSn = Doctrine_Query::create()
         ->select('subject_sn')
         ->from('Student_Model_StudentScoreElectiveGroup')
         ->where('sn=?', $sn)->fetchOne()->subject_sn;


        $q = Doctrine_Query::create()
        ->select('a.* , c.name AS teacher_name, b.year AS year, b.semester AS semester , count(d.sn) AS cc')
        ->from('Student_Model_StudentScoreElectiveGroup a')
        ->leftJoin('a.Student_Model_StudentScoreElectiveStudent d')
        ->leftJoin('a.Semester_Model_SemesterTeacherCourse b')
        ->leftJoin('Teacher_Model_TeacherView c')
        ->where('b.teacher_sn = c.sn');
        if ($onlySemester)
        $q->andwhere('b.year=?', $this->_year)
        ->andWhere('b.semester=?', $this->_semester);

        $q->andWhere('b.grade=?', $grade)
        ->andwhere('a.subject_sn <>?', $subjectSn)
        ->groupBy('a.sn');
        $this->getHelper('layout')->disableLayout();
        $this->view->data = $q->fetchArray();

    }
}

